import Vue from 'vue';

// 引入路由器
import VueRouter from 'vue-router';

// import home from "../views/home.vue";
// import login from "../views/login.vue";

Vue.use(VueRouter);

const router = new VueRouter({
    mode: 'hash',
    routes: [
        {
            path: '/login',
            name: 'login',
            component: () => import('@/views/login.vue'),

            // component:home
        },
        {
            path: '/home',
            name: 'home',
            redirect: '/welcome',
            component: () => import('@/views/home.vue'),

            // component:login
            children: [
                {
                    path: '/welcome',
                    component: () => import('@/views/home/welcome/Welcome.vue')
                },
                {
                    path: '/users',
                    name: 'users',
                    component: () => import('@/views/home/users/Users.vue')
                },
                {
                    path: '/roles',
                    name: 'roles',
                    component: () => import('@/views/home/rolesmanage/RolesList.vue')
                },
                {
                    path: '/rights',
                    name: 'rights',
                    component: () => import('@/views/home/rolesmanage/RiGhtsList.vue')
                },
                {
                    path: '/goods',
                    name: 'goods',
                    component: () => import('@/views/home/goods/Goods.vue')
                },
                {
                    path: '/params',
                    name: 'params',
                    component: () => import('@/views/home/goods/Params.vue')
                },
                {
                    path: '/categories',
                    name: 'categories',
                    component: () => import('@/views/home/goods/Categories.vue')
                },
                {
                    path: '/orders',
                    name: 'orders',
                    component: () => import('@/views/home/order/Orders.vue')
                },
                {
                    path: '/reports',
                    name: 'reports',
                    component: () => import('@/views/home/reports/Reports.vue')
                }

            ]
        },
        {
            path:'/mockdemo',
            name:'mockdemo',
            component:()=>import('@/views/mockDemo/MockDemo.vue')
        },
        {
            path: '/',
            redirect: '/login'
        }

    ]
})

router.beforeEach((to, from, next) => {
    console.log(to);
    console.log(from);
    if (to.path == '/login') {
        // 函数当中return的作用
        return next();
    }
    //不可能是login 
    if (!window.sessionStorage.getItem('token')) {
        return next('/login');
    }
    next();



})

export default router;